package java11;

/*
 * 请你实现一个有四个线程的多线程版  FizzBuzz， 同一个 FizzBuzz 实例会被如下四个线程使用：

线程A将调用 fizz() 来判断是否能被 3 整除，如果可以，则输出 fizz。
线程B将调用 buzz() 来判断是否能被 5 整除，如果可以，则输出 buzz。
线程C将调用 fizzbuzz() 来判断是否同时能被 3 和 5 整除，如果可以，则输出 fizzbuzz。
线程D将调用 number() 来实现输出既不能被 3 整除也不能被 5 整除的数字。
 */
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.IntConsumer;

/**************************************************************
 * 描述:
 *    交替打印字符串
 *    线程A将调用 fizz() 来判断是否能被 3 整除，如果可以，则输出 fizz。
 *    线程B将调用 buzz() 来判断是否能被 5 整除，如果可以，则输出 buzz。
 *    线程C将调用 fizzbuzz() 来判断是否同时能被 3 和 5 整除，如果可以，则输出 fizzbuzz。
 *    线程D将调用 number() 来实现输出既不能被 3 整除也不能被 5 整除的数字。
 *
 *
 **************************************************************/
public class FizzBuzz {
    private int n;

    public FizzBuzz(int n) {
        this.n = n;
    }

    private Lock lock = new ReentrantLock();
    private Condition conditionFizz = lock.newCondition();
    private Condition conditionBuzz = lock.newCondition();
    private Condition conditionFizzBuzz = lock.newCondition();
    private Condition conditionNumber = lock.newCondition();
    int num = 0;


    public void fizz(Runnable printFizz) throws InterruptedException {
        lock.lock();
        try {
            for (int i = 0; i < ((n / 3) - (n / 15)); i++) {

                if (num != 3) {
                    conditionFizz.await();
                }

                printFizz.run();
                num = 0;
                conditionNumber.signal();


            }
        } finally {
            lock.unlock();
        }

    }

    public void buzz(Runnable printBuzz) throws InterruptedException {
        lock.lock();
        try {
            for (int i = 0; i < ((n / 5) - (n / 15)); i++) {

                if (num != 5) {
                    conditionBuzz.await();
                }

                printBuzz.run();
                num = 0;
                conditionNumber.signal();


            }
        } finally {
            lock.unlock();
        }

    }

    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
        lock.lock();
        try {
            for (int i = 0; i < (n / 15); i++) {
                if (num != 15) {
                    conditionFizzBuzz.await();
                }
                printFizzBuzz.run();
                num = 0;
                conditionNumber.signal();


            }

        } finally {
            lock.unlock();
        }

    }

    public void number(IntConsumer printNumber) throws InterruptedException {
        lock.lock();
        try {
            for (int i = 1; i < n + 1; i++) {
                if (num != 0) {
                    conditionNumber.await();
                }
                if (i % 5 == 0 && i % 3 == 0) {
                    num = 15;
                    conditionFizzBuzz.signal();
                } else if (i % 3 == 0) {
                    num = 3;
                    conditionFizz.signal();
                } else if (i % 5 == 0) {
                    num = 5;
                    conditionBuzz.signal();
                } else {
                    printNumber.accept(i);
                }

            }

        } finally {
            lock.unlock();
        }

    }

    public static void main(String[] args) throws InterruptedException {
        FizzBuzz fizzBuzz = new FizzBuzz(16);
        new Thread(() -> {
            try {
                fizzBuzz.fizz(() -> System.out.println("fizz"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "fizz").start();
        new Thread(() -> {
            try {
                fizzBuzz.buzz(() -> System.out.println("buzz"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "buzz").start();
        new Thread(() -> {
            try {
                fizzBuzz.fizzbuzz(() -> System.out.println("fizzBuzz"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "fizzBuzz").start();


        new Thread(() -> {
            try {
                fizzBuzz.number(System.out::println);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "number").start();
    }
}


